package cn.ljq.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

//过滤器
@WebFilter(filterName = "MyFilter" ,urlPatterns = "/*")
public class MyFilter implements Filter {

    public MyFilter() {
        System.out.println("MyFilter()无参构造方法...");
    }

    @Override
    public void doFilter(ServletRequest servletRequest,
                         ServletResponse servletResponse,
                         FilterChain filterChain) throws IOException, ServletException {

        System.out.println("进入到过滤器...");

        //1.session
        //2.请求连接

        //ServletRequest 要做向下转型 HttpServletRequest
        HttpServletRequest req = (HttpServletRequest) servletRequest;

        HttpSession session = req.getSession();

        String servletPath = req.getServletPath();

        Object s = session.getAttribute("DL");



        System.out.println();

        System.out.println("登录的信息 ===" + s);

        System.out.println("session ===" + session);

        System.out.println("请求路径 ===" + servletPath);

        System.out.println();

        //让过滤器继续过滤，直到 Servlet(Countroler)
        filterChain.doFilter(servletRequest,servletResponse); //不想往下走就把这条语句注释掉
        //这条语句是 像返回return 放行  调用下一个过滤器

        System.out.println("======= 离开过滤器 ======");

        //交给下一步，往下走

    }
}
